package com.d17_homework.homework4;

import java.lang.reflect.Field;

public class Demo4 {
    public static void main(String[] args) throws Exception {
        //创建学生类对象
        Student4 stu = new Student4();
        //调用方法，传参
        setProperty(stu,"name","的卢");
        System.out.println(stu);
    }
    public static void setProperty(Object obj,String propertyName,Object value) throws Exception {
        //获得obj的字节码对象
        Class clazz = obj.getClass();
        //获取成员变量
        Field field = clazz.getDeclaredField(propertyName);
        field.setAccessible(true);//取消权限检查
        //调用set方法
        field.set(obj,value);
    }
}
